<?php

namespace App\Admin\Controllers\Behavior;

use App\Models\Behavior\Cat;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CatController extends Controller
{
    use ModelForm;

    public $header = '行为分类';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Cat::class, function (Grid $grid) {

            $grid->model()->orderBy('sort', 'asc');
            $grid->id('编号')->sortable();
            $grid->name('分类名称');
            $grid->sort('排序');
            $grid->created_at('创建时间');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
                $actions->disableDelete();
            });

            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('name', '名称');
                $filter->between('created_at', '添加时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Cat::class, function (Form $form) {

            $form->display('id', '编号');

            $form->text('name', '分类名称')->rules('required');
            $form->number('sort', '排序');

            $form->textarea('description', '分类描述');
            $form->display('created_at', '创建时间');

            $form->disableViewCheck();
        });
    }



}
